// RSA.h: interface for the RSA class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_RSA_H__6985589D_A2DC_4053_A118_D202D0AB181A__INCLUDED_)
#define AFX_RSA_H__6985589D_A2DC_4053_A118_D202D0AB181A__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

namespace RSA
{
	enum
	{
		/*
		bits 1, 2:
		Reserved; must be 0.
		*/
		perm_1 = 1,
		perm_2 = 2,

		/*
		perm_ 3:
		(Revision 2) Print the document.
		(Revision 3) Print the document (possibly not at the highest quality level,
		depending on whether bit 12 is also set).
		*/
		perm_3 = 4,

		/*
		bit 4:
		Modify the contents of the document by operations other than those controlled
		by bits 6, 9, and 11.
		*/
		perm_4 = 8,

		/*
		bit 5:
		(Revision 2) Copy or otherwise extract text and graphics from the document,
		including extracting text and graphics (in support of accessibility	to
		disabled users or for other purposes).
		(Revision 3) Copy or otherwise extract text and graphics from the document
		by operations other than that controlled by bit 10.
		*/
		perm_5 = 16,

		/*
		bit 6:
		Add or modify text annotations, fill in interactive form fields, and, if
		bit 4 is also set, create or modify interactive form fields (including
		signature fields).
		*/
		perm_6 = 32,

		/*
		bits 7, 8:
		Reserved; must be 1.
		*/
		perm_7 = 64,
		perm_8 = 128,

		/*
		bit 9:
		(Revision 3 only) Fill in existing interactive form fields (including
		signature fields), even if bit 6 is clear.
		*/
		perm_9 = 256,

		/*
		bit 10:
		(Revision 3 only) Extract text and graphics (in support of accessibility
		to disabled users or for other purposes).
		*/
		perm_10 = 512,

		/*
		bit 11:
		(Revision 3 only) Assemble the document (insert, rotate, or delete
		pages and create bookmarks or thumbnail images), even if bit 4 is
		clear.
		*/
		perm_11 = 1024,

		/*
		bit 12:
		(Revision 3 only) Print the document to a representation from which
		a faithful digital copy of the PDF content could be generated. When
		this bit is clear (and bit 3 is set), printing is limited to a lowlevel
		representation of the appearance, possibly of degraded quality.
		*/
		perm_12 = 2048,

		/*
		bits 13-32:
		Reserved; must be 1.
		*/
	};

	struct _encrypt
	{
		BYTE	ownerKey[32];	// IN
		BYTE	userKey[32];	// IN
		BYTE	fileId[16];		// IN
		DWORD	permissions;	// IN
		DWORD	revision;		// IN
		DWORD	version;		// IN

		BYTE	fileKey[128];	// OUT
		BYTE	fileKeyLen;		// IN
		BOOL	enabled;
	};

	BOOL	CreatePdfKey(_encrypt& data, const CString& userPassword = "");
	void	CreateNewPdfKey(_encrypt& data, const CString& userPassword = "");
	BOOL	EncryptStream(const _encrypt& data, int objNum, int objGen, LPBYTE pStream, int nStreamLen);


	//=====================================================================
	//	MD5
	typedef unsigned char		uint8;
	typedef unsigned long int	uint32;

	struct md5_context
	{
		uint32	total[2];
		uint32	state[4];
		uint8	buffer[64];
	};

	void md5_starts( md5_context *ctx );
	void md5_update( md5_context *ctx, uint8 *input, uint32 length );
	void md5_finish( md5_context *ctx, uint8 digest[16] );

	//=====================================================================
	//	RC4
	struct rc4_state
	{
		INT x, y, m[256];
	};

	void rc4_setup( struct rc4_state *s, BYTE *key,  INT length );
	void rc4_crypt( struct rc4_state *s, BYTE *data, INT length );
};

#endif // !defined(AFX_RSA_H__6985589D_A2DC_4053_A118_D202D0AB181A__INCLUDED_)
